﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BO;
using Csla;
using System.ComponentModel;

namespace UI
{
    internal class AppContext:System.Windows.Forms.ApplicationContext
    {
        private static AppContext _Currrent;
        public static AppContext Currrent
        {

            set { _Currrent = value; }
            get { return _Currrent; }
        }

        private Form _MainForm;
        public Form MainForm
        {
            
            get { return _MainForm; }
        }
        BackgroundWorker _Worker;
        //public BackgroundWorker Worker
        //{
        //    get { return _Worker; }
        //}
        private AppContext()
        {
            //forbide use this method.
        }
        public AppContext(Form pForm):base(pForm)
        {
            _MainForm = pForm;
            _MainForm.FormClosed += _MainForm_FormClosed;
            _Worker = new BackgroundWorker();
            _Worker.WorkerSupportsCancellation = true;
            _Worker.DoWork += _Worker_DoWork;
            _Worker.RunWorkerAsync();
        }

        void _Worker_DoWork(object sender, DoWorkEventArgs e)
        {
          
            RefreshCompanyList();
            RefreshCenterList();
            RefreshDepartmentList();
            RefreshProductGroupList();
            RefreshProductList();
            RefreshStaffList();
        }

        void _MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_Worker.IsBusy)
            {
                _Worker.CancelAsync();
            }
            Application.Exit();
        }
        private static StaffList _StaffList;
        public static StaffList StaffList
        {
            get
            {
                if (_StaffList == null)
                {
                    _StaffList = ListFactory<StaffList>.GetList(GlobalVar.User.CompanyFilter);
                }
                return _StaffList;
            }
        }
        public static void RefreshStaffList()
        {
            _StaffList = ListFactory<StaffList>.GetList(GlobalVar.User.CompanyFilter);
        }
        private static CompanyList _CompanyList;
        public static CompanyList CompanyList
        {
            get
            {
                if (_CompanyList == null)
                {
                    _CompanyList = ListFactory<CompanyList>.GetList("");
                }
                return _CompanyList;
            }
        }
        public static void RefreshCompanyList()
        {
            _CompanyList = ListFactory<CompanyList>.GetList("");
        }
        private static CenterList _CenterList;
        public static CenterList CenterList
        {
            get
            {
                if (_CenterList == null)
                {
                    _CenterList = ListFactory<CenterList>.GetList(GlobalVar.User.CompanyFilter);
                }
                return _CenterList;
            }
        }
        public static void RefreshCenterList()
        {
            _CenterList = ListFactory<CenterList>.GetList(GlobalVar.User.CompanyFilter);
        }
      
        private static DepartmentList _DepartmentList;
        public static DepartmentList DepartmentList
        {
            get
            {
                if (_DepartmentList == null)
                {
                    _DepartmentList = ListFactory<DepartmentList>.GetList(GlobalVar.User.CompanyFilter);
                }
                return _DepartmentList;
            }
        }

        public static void RefreshDepartmentList()
        {
            _DepartmentList = ListFactory<DepartmentList>.GetList(GlobalVar.User.CompanyFilter);
        }

        private static ProductList _ProductList;
        public static ProductList ProductList
        {
            get
            {
                if (_ProductList == null)
                {
                    _ProductList = ListFactory<ProductList>.GetList("");
                }
                return _ProductList;
            }
        }
        public static void RefreshProductList()
        {
            _ProductList = ListFactory<ProductList>.GetList("");
        }
        private static ProductGroupList _ProductGroupList;
        public static ProductGroupList ProductGroupList
        {
            get
            {
                if (_ProductGroupList == null)
                {
                    _ProductGroupList = ListFactory<ProductGroupList>.GetList("");
                }
                return _ProductGroupList;
            }
        }
        public static void RefreshProductGroupList()
        {
            _ProductGroupList = ListFactory<ProductGroupList>.GetList("");
        }

    }
}
